﻿// Decompiled with JetBrains decompiler
// Type: DnProcessSimulateCommands.TcCommands.TcReplace.CUiReplaceDialog
// Assembly: DnProcessSimulateCommands, Version=2402.0.0.890, Culture=neutral, PublicKeyToken=null
// MVID: A885D881-AEDF-447E-852A-6F13102E089C
// Assembly location: C:\Program Files\Tecnomatix_2402\eMPower\DotNetCommands\DnProcessSimulateCommands.dll

using EngineeringInternalExtension;
using EngineeringInternalExtension.Utilities;
using System;
using System.CodeDom.Compiler;
using System.Collections;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Diagnostics;
using System.Resources;
using System.Windows;
using System.Windows.Forms;
using System.Windows.Input;
using System.Windows.Markup;
using Tecnomatix.Engineering;
using Tecnomatix.Engineering.Ui;
using Tecnomatix.Engineering.Ui.WPF;
using Tecnomatix.Engineering.Utilities;


namespace DnProcessSimulateCommands.TcCommands.TcReplace;

public class CUiReplaceDialog : TxWindow, IComponentConnector
{
  private CApReplaceObjectManager m_replaceManager;
  private bool m_bClassificationObjectLoaded;
  private CCoTcReplaceBaseCmd _cmd;
  private bool _result;
  private CApReplaceWhatValidator m_SourceValidator;
  private CApReplaceWithValidator m_ReplacementValidator;
  private List<ITxObject> _failedObjects = new List<ITxObject>();
  internal CUiReplaceDialog ReplaceDialog;
  internal TxObjectGridControl grInstResources;
  internal TxObjEditBoxControl pbReplaceBy;
  internal System.Windows.Controls.Button btnReplace;
  internal System.Windows.Controls.Button btnCancel;
  private bool _contentLoaded;

  public bool Result
  {
    get => this._result;
    set => this._result = value;
  }

  public List<ITxObject> FailedObjects
  {
    get => this._failedObjects;
    set => this._failedObjects = value;
  }

  public CUiReplaceDialog(CCoTcReplaceBaseCmd cmd)
  {
    this._cmd = cmd;
    this.InitializeComponent();
    this.m_replaceManager = new CApReplaceObjectManager();
    this.m_SourceValidator = new CApReplaceWhatValidator(this);
    this.m_ReplacementValidator = new CApReplaceWithValidator(this);
  }

  private void CUiReplaceDialog_Loaded(object sender, RoutedEventArgs e)
  {
    this.InitializePicker(this.pbReplaceBy);
    this.InitGridColData();
    this._cmd.DialogIsOpen(true);
    this.grInstResources.ObjectInserted += new TxObjGridCtrl_ObjectInsertedEventHandler(this.grInstResources_ObjectInserted);
    this.grInstResources.RowDeleted += new TxObjGridCtrl_RowDeletedEventHandler(this.grInstResources_RowDeleted);
    this.InitResInstGrid();
    if (((Collection<ITxObject>) this.grInstResources.Objects).Count > 0)
      this.pbReplaceBy.Focus();
    this.UpdateEnableReplaceBtnState();
  }

  private void CUiReplaceDialog_Closing(object sender, CancelEventArgs e)
  {
    this.FinalizePicker(this.pbReplaceBy);
    this.grInstResources.ObjectInserted -= new TxObjGridCtrl_ObjectInsertedEventHandler(this.grInstResources_ObjectInserted);
    this.grInstResources.RowDeleted -= new TxObjGridCtrl_RowDeletedEventHandler(this.grInstResources_RowDeleted);
    this._cmd.DialogIsOpen(false);
    ((Collection<ITxObject>) this.grInstResources.Objects).Clear();
    this.grInstResources.UnInitialize();
    this.m_replaceManager.UnLoadObjects();
  }

  private void InitResInstGrid()
  {
    TxObjectList allItems = TxApplication.ActiveSelection.GetAllItems();
    for (int index = 0; index < ((Collection<ITxObject>) allItems).Count; ++index)
    {
      string errorMessage = string.Empty;
      if (this.m_SourceValidator.IsValidObject(((Collection<ITxObject>) allItems)[index], out errorMessage))
        this.grInstResources.AppendObject(((Collection<ITxObject>) allItems)[index]);
    }
  }

  private void btnReplace_Click(object sender, RoutedEventArgs e)
  {
    bool flag = false;
    if (this.pbReplaceBy.Object != null && this.pbReplaceBy.Object.IsValid() && this.grInstResources != null && ((Collection<ITxObject>) this.grInstResources.Objects).Count > 0)
    {
      System.Windows.Forms.Cursor current = System.Windows.Forms.Cursor.Current;
      System.Windows.Forms.Cursor.Current = System.Windows.Forms.Cursors.WaitCursor;
      try
      {
        TxErrorStack.Clear();
        flag = TxTcUtilities.ReplacePrototype(this.grInstResources.Objects, this.pbReplaceBy.Object);
        flag = this.ShowReplaceErrors() & flag;
      }
      catch (Exception ex)
      {
        flag = false;
        int num = (int) TxMessageBox.Show(ex.Message, this._cmd.Name, MessageBoxButtons.OK, MessageBoxIcon.Hand);
        TxErrorStackDlg.DisplayErrors();
      }
      finally
      {
        System.Windows.Forms.Cursor.Current = current;
        this.Result = flag;
      }
    }
    this.m_replaceManager.UnLoadObjects();
    ((Window) this).Close();
  }

  private void pbReplaceBy_Picked(object sender, TxObjEditBoxCtrl_PickedEventArgs args)
  {
    this.UpdateEnableReplaceBtnState();
    if (this.m_bClassificationObjectLoaded)
      return;
    this.m_replaceManager.UnLoadObjects();
  }

  private void picker_KeyUp(object sender, System.Windows.Input.KeyEventArgs e)
  {
    if (sender is TxObjEditBoxControl && (e.Key == Key.Delete || e.Key == Key.Back) && (TxObjEditBoxControl) sender == this.pbReplaceBy)
      this.pbReplaceBy.Object = (ITxObject) null;
    this.m_replaceManager.UnLoadObjects();
  }

  private void InitGridColData()
  {
    TxGridColumnData txGridColumnData = new TxGridColumnData();
    string prototypeGridHeader = StringTable.REPLACE_PROTOTYPE_GRID_HEADER;
    txGridColumnData.Init(prototypeGridHeader, (TxGridColumnType) 4);
    TxGridColumnDataCollection columnDataCollection = new TxGridColumnDataCollection();
    columnDataCollection.AddItem(txGridColumnData);
    TxGridDescription txGridDescription = new TxGridDescription();
    txGridDescription.SetData(columnDataCollection, 0);
    this.grInstResources.Init(txGridDescription);
    this.grInstResources.ListenToPick = true;
    this.grInstResources.PickLevel = (TxPickLevel) 1;
    this.grInstResources.SetValidator((ITxValidator) this.m_SourceValidator);
  }

  private void InitializePicker(TxObjEditBoxControl picker)
  {
    picker.ListenToPick = true;
    picker.PickLevel = (TxPickLevel) 1;
    picker.PickOnly = true;
    picker.SetValidator((ITxValidator) this.m_ReplacementValidator);
    picker.Picked += new TxObjEditBoxCtrl_PickedEventHandler(this.pbReplaceBy_Picked);
    ((UIElement) picker).KeyUp += new System.Windows.Input.KeyEventHandler(this.picker_KeyUp);
  }

  private void FinalizePicker(TxObjEditBoxControl picker)
  {
    picker.Picked -= new TxObjEditBoxCtrl_PickedEventHandler(this.pbReplaceBy_Picked);
    ((UIElement) picker).KeyUp -= new System.Windows.Input.KeyEventHandler(this.picker_KeyUp);
    picker.UnInitialize();
  }

  private void btnCancel_Click(object sender, RoutedEventArgs e) => ((Window) this).Close();

  private void UpdateEnableReplaceBtnState()
  {
    this.btnReplace.IsEnabled = ((Collection<ITxObject>) this.grInstResources.Objects).Count > 0 && this.pbReplaceBy.Object != null;
  }

  private void grInstResources_ObjectInserted(
    object sender,
    TxObjGridCtrl_ObjectInsertedEventArgs args)
  {
    this.UpdateEnableReplaceBtnState();
  }

  private void grInstResources_RowDeleted(object sender, TxObjGridCtrl_RowDeletedEventArgs args)
  {
    this.UpdateEnableReplaceBtnState();
  }

  private void btnCassificationSearch_Click(object sender, RoutedEventArgs e)
  {
    string errorMessage = string.Empty;
    if (((Collection<ITxObject>) TxApplication.ActiveDocument.PhysicalRoot.GetAllDescendants((ITxTypeFilter) new TxTypeFilter(typeof (TxCompoundResource)))).Count == 0)
    {
      int num1 = (int) TxMessageBox.ShowModal(StringTable.REPLACE_PROTOTYPE_NO_WORKAREAS_LOADED_ERROR, this._cmd.Name, MessageBoxButtons.OK, MessageBoxIcon.Hand);
    }
    else
    {
      CUiClassificationSearchWindow classificationSearchWindow = new CUiClassificationSearchWindow(this._cmd);
      ((Window) classificationSearchWindow).Owner = Window.GetWindow((DependencyObject) this);
      bool? nullable = ((Window) classificationSearchWindow).ShowDialog();
      if (!nullable.HasValue || !nullable.Value)
        return;
      this.m_replaceManager.UnLoadObjects();
      this.m_replaceManager.LoadObject(classificationSearchWindow.SelectedObjectExternalId);
      ITxObject loadedObject = this.m_replaceManager.LoadedObject;
      if (this.m_ReplacementValidator.IsValidObject(loadedObject, out errorMessage))
      {
        this.m_bClassificationObjectLoaded = true;
        this.pbReplaceBy.Object = loadedObject;
        this.m_bClassificationObjectLoaded = false;
        this.UpdateEnableReplaceBtnState();
      }
      else
      {
        int num2 = (int) TxMessageBox.ShowModal(errorMessage, this._cmd.Name, MessageBoxButtons.OK, MessageBoxIcon.Hand);
        TxErrorStackDlg.DisplayErrors();
      }
    }
  }

  private bool ShowReplaceErrors()
  {
    bool flag = true;
    if (TxErrorStack.Contains("TCSOACLIENT_REPLACE_ITEMS_PARTIAL_ERROR"))
    {
      flag = false;
      Hashtable hashtable = new Hashtable();
      List<string> stringList = new List<string>();
      foreach (IList<string> allParameter in TxErrorStack.GetAllParameters("TCSOACLIENT_REPLACE_ITEMS_PARTIAL_ERROR"))
        stringList.AddRange((IEnumerable<string>) allParameter);
      hashtable.Add((object) StringTable.REPLACE_PROTOTYPE_FAILURES, (object) stringList.ToArray());
      TxErrorWindow.ShowDialog(this._cmd.Name, StringTable.REPLACE_PROTOTYPE_FAILED_SUBTITLE, hashtable, new Hashtable(), true);
    }
    return flag;
  }

  internal ResourceManager GetResourceManager()
  {
    return new ResourceManager("DnProcessSimulateCommands.TcCommands.TcReplace.StringTable", ((object) this).GetType().Assembly);
  }

  [DebuggerNonUserCode]
  [GeneratedCode("PresentationBuildTasks", "4.0.0.0")]
  public void InitializeComponent()
  {
    if (this._contentLoaded)
      return;
    this._contentLoaded = true;
    System.Windows.Application.LoadComponent((object) this, new Uri("/DnProcessSimulateCommands;component/tccommands/tcreplace/cuireplacedialog.xaml", UriKind.Relative));
  }

  [DebuggerNonUserCode]
  [GeneratedCode("PresentationBuildTasks", "4.0.0.0")]
  [EditorBrowsable(EditorBrowsableState.Never)]
  void IComponentConnector.Connect(int connectionId, object target)
  {
    switch (connectionId)
    {
      case 1:
        this.ReplaceDialog = (CUiReplaceDialog) target;
        ((FrameworkElement) this.ReplaceDialog).Loaded += new RoutedEventHandler(this.CUiReplaceDialog_Loaded);
        ((Window) this.ReplaceDialog).Closing += new CancelEventHandler(this.CUiReplaceDialog_Closing);
        break;
      case 2:
        this.grInstResources = (TxObjectGridControl) target;
        break;
      case 3:
        ((System.Windows.Controls.Primitives.ButtonBase) target).Click += new RoutedEventHandler(this.btnCassificationSearch_Click);
        break;
      case 4:
        this.pbReplaceBy = (TxObjEditBoxControl) target;
        break;
      case 5:
        this.btnReplace = (System.Windows.Controls.Button) target;
        this.btnReplace.Click += new RoutedEventHandler(this.btnReplace_Click);
        break;
      case 6:
        this.btnCancel = (System.Windows.Controls.Button) target;
        this.btnCancel.Click += new RoutedEventHandler(this.btnCancel_Click);
        break;
      default:
        this._contentLoaded = true;
        break;
    }
  }
}
